﻿using Abp.Domain.Repositories;
using Project.Apps.DataExporting.Dto;
using Project.Entities;
using System;
using System.Collections.Generic;
using TransportationMonitor.DataExporting.Excel.EpPlus;

namespace Project.Apps.DataExporting
{
    public class WorkStockOutBillExporter : EpPlusExcelExporterBase, IWorkStockOutBillExporter
    {

        private readonly IRepository<WorkStockOutBill, long> _workStockOutBillRepository;
        private readonly IRepository<WorkStockOutBillDetail, long> _workStockOutBillDetailRepository;

        public WorkStockOutBillExporter(IRepository<WorkStockOutBill, long> workStockOutBillRepositor, IRepository<WorkStockOutBillDetail, long> workStockOutBillDetailRepository)
        {
            _workStockOutBillRepository = workStockOutBillRepositor;
            _workStockOutBillDetailRepository = workStockOutBillDetailRepository;
        }

        public FileDto ExportToFile(WorkStockOutBillDto input)
        {
            IList<WorkStockOutBill> allList = new List<WorkStockOutBill>();
            allList = _workStockOutBillRepository.GetAllList();
            foreach (var b in allList)
            {
                b.Details = _workStockOutBillDetailRepository.GetAllList(x => x.BillId == b.Id);
            }

            /* 单张订单
            WorkStockOutBill bill = _workStockOutBillRepository.FirstOrDefault(x => x.Id == input.Id);
            bill.Details = null;
            if (bill != null)
            {
                bill.Details = _workStockOutBillDetailRepository.GetAllList(x => x.BillId == bill.Id);
            }
            */

            var filename = "出库单_" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".xlsx";
            return CreateExcelPackage(
                filename,
                excelPackage =>
                {
                    var sheet = excelPackage.Workbook.Worksheets.Add("出库单");
                    sheet.OutLineApplyStyle = true;

                    #region 多张订单

                    var headerIndex = 1;

                    foreach (var b in allList)
                    {
                        //订单信息
                        sheet.Cells[headerIndex, 1].Value = "部门：";
                        sheet.Cells[headerIndex, 1].Style.Font.Bold = true;
                        sheet.Cells[headerIndex, 1].AutoFitColumns();//单元格宽度自适应
                        sheet.Cells[headerIndex, 2, headerIndex, 4].Merge = true;//合并单元格
                        sheet.Cells[headerIndex, 2].Value = b.DepartmentName;

                        sheet.Cells[headerIndex, 5].Value = "供应商人员：";
                        sheet.Cells[headerIndex, 5].Style.Font.Bold = true;
                        sheet.Cells[headerIndex, 5].AutoFitColumns();
                        sheet.Cells[headerIndex, 6, headerIndex, 8].Merge = true;
                        sheet.Cells[headerIndex, 6].Value = b.SupplierEmployeeName;

                        //sheet.Cells[headerIndex, 1, headerIndex, 8].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.DashDot);


                        //sheet.Row(2).Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin);
                        //sheet.Row(3).Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin);

                        //sheet.Tables[0].TableStyle = OfficeOpenXml.Table.TableStyles.Dark1;


                        //订单明细
                        //表头
                        headerIndex += 1;
                        AddHeader(
                            sheet,
                            headerIndex, true,
                            "数量",
                            "金额",
                            "仓库"
                        );

                        //sheet.Cells[headerIndex, 1, headerIndex, 8].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.DashDot);
                        //内容
                        headerIndex += 1;
                        AddObjects(
                            sheet,
                            headerIndex,
                            b.Details, true,
                            _ => _.Number,
                            _ => _.Price,
                            _ => _.WarehouseName
                            );


                        headerIndex += b.Details.Count + 2;
                    }

                    #endregion

                    /* 单张订单
                    //订单信息
                    sheet.Cells[1, 1].Value = "部门：";
                    sheet.Cells[1, 1].Style.Font.Bold = true;
                    sheet.Cells[1, 1].AutoFitColumns();//单元格宽度自适应
                    sheet.Cells[1, 2, 1, 4].Merge = true;//合并单元格
                    sheet.Cells[1, 2].Value = bill.DepartmentName;

                    sheet.Cells[1, 5].Value = "供应商人员：";
                    sheet.Cells[1, 5].Style.Font.Bold = true;
                    sheet.Cells[1, 5].AutoFitColumns();
                    sheet.Cells[1, 6, 1, 8].Merge = true;
                    sheet.Cells[1, 6].Value = bill.SupplierEmployeeName;

                    //订单明细
                    int headerIndex = 3;
                    //表头
                    AddHeader(
                        sheet,
                        headerIndex,
                        "数量",
                        "金额",
                        "仓库"
                    );
                    //内容
                    AddObjects(
                        sheet,
                        headerIndex + 1,
                        bill.Details,
                        _ => _.Number,
                        _ => _.Price,
                        _ => _.WarehouseName
                        );
                        */

                    ////Formatting cells 时间列的格式
                    //var timeColumn = sheet.Column(1);
                    //timeColumn.Style.Numberformat.Format = "mm-dd-yy hh:mm:ss";

                    //for (var i = 1; i <= 10; i++)
                    //{
                    //    if (i.IsIn(5, 10)) //Don't AutoFit Parameters and Exception
                    //    {
                    //        continue;
                    //    }

                    //    sheet.Column(i).AutoFit();
                    //}
                });
        }
    }
}